package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Override
    public Boolean selectCountByCategoryId(Long categoryId) {
        Integer integer = setmealMapper.selectCountByCategoryId(categoryId);

        return integer>0;
    }

    @Override
    public Page<SetmealDto> selectAll(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<SetmealDto> setmealDtos = setmealMapper.selectAll(name);
        PageInfo pageInfo = new PageInfo(setmealDtos);

        for (SetmealDto setmealDto : setmealDtos) {
            Category category = categoryMapper.selectByCategoryId(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
        }

        Page<SetmealDto> setmealDtoPage = new Page<>();

        setmealDtoPage.setTotal(pageInfo.getTotal());
        setmealDtoPage.setRecords(setmealDtos);

        return setmealDtoPage;

    }

    @Override
    @Transactional
    public Boolean addSetmeal(SetmealDto setmealDto) {
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());

        Integer integer = setmealMapper.addSetmeal(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateTime(LocalDateTime.now());
        }

        Integer integer1 = setmealDishMapper.addSetmealDish(setmealDishes);


        return (integer>0 && integer1>0);
    }

    @Override
    public SetmealDto findById(Long id) {
        Setmeal setmeal = setmealMapper.findById(id);
        List<SetmealDish> bySetmealId = setmealDishMapper.findBySetmealId(setmeal.getId());

        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setSetmealDishes(bySetmealId);
        BeanUtils.copyProperties(setmeal, setmealDto);

        return setmealDto;
    }

    @Override
    @Transactional
    public Boolean update(SetmealDto setmealDto) {
        setmealDto.setUpdateTime(LocalDateTime.now());
        Integer update = setmealMapper.update(setmealDto);

        Integer delete = setmealDishMapper.delete(setmealDto.getId());

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
        }
        Integer integer = setmealDishMapper.addSetmealDish(setmealDto.getSetmealDishes());

        return true;



    }

    @Override
    public Boolean updateStatus(Integer status, Long[] ids) {
        Integer integer = setmealMapper.updateStatus(status, ids);
        return integer>0;
    }

    @Override
    public Boolean deleteByIds(Long[] ids) {
        Integer delete = setmealMapper.delete(ids);
        return delete>0;
    }

    @Override
    public List<SetmealDto> findByCategoryIdAndStatus(Long categoryId, Integer status) {
        List<SetmealDto> setmealDtos = setmealMapper.findByCategoryIdAndStatus(categoryId, status);
        return setmealDtos;
    }

}
